package parser.prestige;

import parser.Parser;
import parser.prestige.AbstractNode;
import parser.prestige.IXmlElement;
import parser.prestige.meldunek.xml.*;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: marcin.kaluza
 * Date: 29.11.13
 * Time: 09:55
 * To change this template use File | Settings | File Templates.
 */
public class MeldunekParser extends Parser {
    @Override
    public List<AbstractNode> parseInput(List<String> lines, String inputFileName) {
        System.out.println("MeldunekParser:parsingInput");
        for (String line : lines) {
            System.out.println("\t"+line);
        }
        //return null;
        List<AbstractNode> meldunki = new ArrayList<AbstractNode>();
        meldunki.add(new Meldunek(
                "test",
                "test",
                "test",
                "test",
                "test",
                "test",
                "test",
                "test",
                "test",
                "test",
                "test",
                new Zasob(
                        "kodRodzajZasobu",
                        "rodzajZasobu",
                        "kodZasobu",
                        "zasob",
                        "uwagi"
                ),
                new Czynnosc(
                        "czynnosc",
                        "kod czynnosci",
                        "lp czynnosci",
                        "ilosc",
                        "czas",
                        "stawka",
                        "koszt",
                        new Pracownik(
                                "pracownik",
                                "ilosc",
                                "koszt"
                        )
                ),
                new Material(
                        "indks",
                        "nazwa",
                        "idDostawa",
                        "nrDostawy",
                        "idMagazyn",
                        "ilosc",
                        "ilosc TPP",
                        "precyzja",
                        "narzut",
                        "cena",
                        "wartosc"
                ),
                new MaterialDodatkowy(
                        "indeks",
                        "nazwa",
                        "ilosc",
                        "ilosc TPP",
                        "precyzja",
                        "narzut",
                        "cena",
                        "wartosc"
                ),
                new Odpad(
                        "indeks",
                        "nazwa",
                        "idMagazyn",
                        "ilosc",
                        "ilosc TPP",
                        "precyzja",
                        "narzut",
                        "cena",
                        "wartosc"
                ),
                new Produkt(
                        "indeks",
                        "nazwa",
                        "idMagazyn",
                        "ilosc",
                        "ilosc TPP",
                        "precyzja",
                        "narzut",
                        "cena",
                        "wartosc"
                )
        ));
        return meldunki;
    }

    @Override
    public void writeOutput(List<AbstractNode> nodes) {
        System.out.println("MeldunekParser:writingOutput");
        XMLOutputFactory outputfactory = XMLOutputFactory.newInstance();
        try {
            XMLEventWriter eventWriter = outputfactory.createXMLEventWriter(
                    new FileOutputStream((outputFileName == null) ? inputFileName : outputFileName));
            XMLEventFactory eventFactory = XMLEventFactory.newInstance();

            XMLEvent endLineEvent = eventFactory.createDTD("\n");
            XMLEvent tabulatorEvent = eventFactory.createDTD("\t");

            StartDocument startDocument = eventFactory.createStartDocument();//TU MOZNA DOROBIC KODOWANIE
            eventWriter.add(startDocument);

            //lamanie lini po xml version ...
            eventWriter.add(endLineEvent);

            //TAG: <Meldunki>
            StartElement meldunkiStartElement = eventFactory.createStartElement("", "", "Meldunki");
            eventWriter.add(meldunkiStartElement);
            eventWriter.add(endLineEvent);

            //TAG:<NumerWersjiXML>
            eventWriter.add(tabulatorEvent);
            StartElement startElement = eventFactory.createStartElement("", "", "NumerWersjiXML");
            eventWriter.add(startElement);
            eventWriter.add(eventFactory.createCharacters("1"));
            eventWriter.add(eventFactory.createEndElement("","","NumerWersjiXML"));
            eventWriter.add(endLineEvent);

//            for (Meldunek meldunek : meldunki) {
//                ((IXmlElement)meldunek).createAsNode(eventWriter, 1);
//            }
            for (AbstractNode node : nodes) {
                Meldunek meldunek = (Meldunek)node;
                ((IXmlElement)meldunek).createAsNode(eventWriter, 1);
            }

            EndElement meldunkiEndElement = eventFactory.createEndElement("", "", "Meldunki");
            eventWriter.add(meldunkiEndElement);
            eventWriter.add(endLineEvent);

            //zapisaine dokumentu
            eventWriter.add(eventFactory.createEndDocument());
            eventWriter.close();
        } catch (XMLStreamException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
